package cn.tedu.mvcaddress.controller;

import cn.tedu.mvcaddress.mapper.AddressMapper;
import cn.tedu.mvcaddress.pojo.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address/")
public class AddressController {

    @Autowired(required = false)
    private AddressMapper addressMapper;

    /**新增地址*/
    @RequestMapping("insert")
    public String insertAddress(Address address){
        addressMapper.insert(address);

        return "添加成功!<a href='/'>返回首页</a>";
    }

    /**查询地址*/
    @RequestMapping("select")
    public String selectAddress(){
        List<Address> list = addressMapper.select();

        String html = "<table border='1'>";
        html += "<caption>地址列表</caption>";
        html += "<tr><th>id</th><th>收件人</th><th>收件地址</th><th>邮箱</th><th>手机号</th><th>标签</th><th>操作</th></tr>";
        for (Address a: list) {
            html += "<tr>";
            html += "<td>" + a.getId() +  "</td>";
            html += "<td>" + a.getReceiver() +  "</td>";
            html += "<td>" + a.getAddress() +  "</td>";
            html += "<td>" + a.getEmail() +  "</td>";
            html += "<td>" + a.getMobile() +  "</td>";
            html += "<td>" + a.getTag() +  "</td>";
            html += "<td><a href='/address/delete?id=" + a.getId() + "'>删除</a></td>";
            html += "</tr>";
        }

        html += "</table><a href='/'>返回首页</a>";

        return html;
    }

    /**删除地址*/
    @RequestMapping("delete")
    public String deleteAddress(int id){
        addressMapper.delete(id);

        return "删除成功!<a href='/address/select'>返回列表页</a>";
    }

    /**修改地址*/
    @RequestMapping("update")
    public String updateAddress(Address address){
        addressMapper.update(address);

        return "修改成功!<a href='/address/select'>返回列表页</a>";
    }
}






